<?php

namespace App\Http\Controllers\Admin;

use App\Enums\AvailableStatus;
use App\Http\Controllers\Controller;
use App\Models\Agent;
use App\Models\AgentConfig;
use App\Models\AgentUser;
use App\Services\AgentService;
use Vinlon\Laravel\LayAdmin\Exceptions\AdminException;

class AgentController extends Controller
{
    /** @var AgentService */
    private $agentService;

    /**
     * AgentController constructor.
     */
    public function __construct(AgentService $agentService)
    {
        $this->agentService = $agentService;
    }

    /** 列表查询 */
    public function index()
    {
        $query = Agent::query()
            ->when(request()->keyword, function ($q) {
                return $q->where('name', 'like', '%' . request()->keyword . '%');
            })
            ->with(['admin_user'])
            ->orderByDesc('id');

        return paginate_result($query);
    }

    /** 添加或修改 */
    public function store()
    {
        request()->validate([
            'name' => 'required',
        ]);
        $agentName = request()->name;
        if (request()->id) {
            $agent = Agent::query()->find(request()->id);
            $agent->name = $agentName;
            $agent->save();

            if (request()->password) {
                // 重置密码
                $user = $agent->admin_user;
                $user->password = \Hash::make(request()->password);
                $user->save();
            }
        } else {
            request()->validate([
                'user_name' => 'required',
                'password' => 'required',
            ]);
            $userName = request()->user_name;
            if (AgentUser::query()->where('username', $userName)->count() > 0) {
                throw new AdminException('用户名已存在');
            }

            $agent = new Agent();
            $agent->name = $agentName;
            $agent->app_id = uniqid();
            $agent->app_secret = md5($agent->name . $agent->app_id);
            $agent->save();

            // 创建代理商用户
            $user = new AgentUser();
            $user->agent_id = $agent->id;
            $user->username = $userName;
            $user->password = \Hash::make(request()->password);
            $user->save();
        }
    }

    /** 删除 */
    public function destroy($id)
    {
        $agent = Agent::query()->find($id);
        $agent->delete();
    }

    /** 修改代理状态 */
    public function changeStatus($id)
    {
        request()->validate([
            'status' => 'enum_value:' . AvailableStatus::class,
        ]);
        $agent = Agent::query()->find($id);
        $agent->status = request()->status;
        $agent->save();
    }

    /** 修改代理配置 */
    public function saveAgentSettings($id)
    {
        $params = request()->validate([
            AgentConfig::PAY_MERCHANT_NO => 'nullable',
            AgentConfig::PAY_RESALE_AGENT_FEE_RATE => 'required',
            AgentConfig::PAY_SERVICE_FEE_RATE => 'required',
            AgentConfig::PAY_RESALE_SERVICE_FEE_RATE => 'required',

            AgentConfig::PAY_CHANNEL => 'nullable',
            AgentConfig::ADAPAY_APP_ID => 'nullable',
            AgentConfig::ADAPAY_DIVIDE_ID => 'nullable',
            AgentConfig::ADAPAY_API_KEY => 'nullable',
            AgentConfig::ADAPAY_RSA_PUBLIC_KEY => 'nullable',
            AgentConfig::ADAPAY_RSA_PRIVATE_KEY => 'nullable',

            AgentConfig::SDPAY_MERCH_NO => 'nullable',
            AgentConfig::SDPAY_RSA_PRIVATE_KEY => 'nullable',
        ]);

        $this->agentService->saveAgentSettings($id, $params);
    }

    /** 查询代理配置 */
    public function getAgentSettings($id)
    {
        return $this->agentService->getAgentSettings($id, [
            AgentConfig::PAY_MERCHANT_NO,
            AgentConfig::PAY_RESALE_AGENT_FEE_RATE,
            AgentConfig::PAY_SERVICE_FEE_RATE,
            AgentConfig::PAY_RESALE_SERVICE_FEE_RATE,
            AgentConfig::PAY_CHANNEL,
            AgentConfig::ADAPAY_APP_ID,
            AgentConfig::ADAPAY_DIVIDE_ID,
            AgentConfig::ADAPAY_API_KEY,
            AgentConfig::ADAPAY_RSA_PUBLIC_KEY,
            AgentConfig::ADAPAY_RSA_PRIVATE_KEY,

            AgentConfig::SDPAY_MERCH_NO,
            AgentConfig::SDPAY_RSA_PRIVATE_KEY,
        ]);
    }
}
